package com.cskaoyan.wordmemorize.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cskaoyan.wordmemorize.converter.AIReadConverter;
import com.cskaoyan.wordmemorize.dao.entity.UserVocAISituationDO;
import com.cskaoyan.wordmemorize.dao.entity.UserVocAISituationOptionDO;
import com.cskaoyan.wordmemorize.dao.mapper.UserVocAISituationMapper;
import com.cskaoyan.wordmemorize.dao.mapper.UserVocAISituationOptionMapper;
import com.cskaoyan.wordmemorize.dto.admin.PageDTO;
import com.cskaoyan.wordmemorize.dto.admin.UserVocAISituationDTO;
import com.cskaoyan.wordmemorize.request.PageRequest;
import com.cskaoyan.wordmemorize.request.UserVocAISituationCommand;
import com.cskaoyan.wordmemorize.service.AIReadService;
import org.apache.ibatis.executor.BatchExecutorException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 顾海龙
 * @version 1.0
 */
@Service
public class AIReadServiceImpl implements AIReadService {

    @Autowired
    UserVocAISituationMapper userVocAISituationMapper;

    @Autowired
    UserVocAISituationOptionMapper userVocAISituationOptionMapper;

    @Autowired
    AIReadConverter aiReadConverter;


    //添加AI阅读场景以及选项信息(后台等级特权管理-AI阅读)
    @Override
    public void addAISituation(UserVocAISituationCommand command) throws BatchExecutorException {
        UserVocAISituationDO userVocAISituationDO = aiReadConverter.userVocAISituationCommandToDO(command);
        userVocAISituationMapper.insert(userVocAISituationDO);
        List<UserVocAISituationOptionDO> options = userVocAISituationDO.getOptions();
        for (UserVocAISituationOptionDO option : options) {
            option.setSituationId(userVocAISituationDO.getId());
        }
        userVocAISituationOptionMapper.insert(options);
    }


    //更新AI阅读场景以及选项信息(后台等级特权管理-AI阅读)
    @Override
    public void updateAISituation(UserVocAISituationCommand command) throws BatchExecutorException {
        UserVocAISituationDO userVocAISituationDO = aiReadConverter.userVocAISituationCommandToDO(command);
        List<UserVocAISituationOptionDO> options = userVocAISituationDO.getOptions();
        userVocAISituationOptionMapper.updateById(options);
        userVocAISituationMapper.updateById(userVocAISituationDO);
    }


    //删除AI阅读场景(后台等级特权管理-AI阅读)
    @Override
    public void deleteAISituation(Long id) throws BatchExecutorException {
        QueryWrapper<UserVocAISituationOptionDO> userVocAISituationOptionDOQueryWrapper = new QueryWrapper<>();
        userVocAISituationOptionDOQueryWrapper.eq("situation_id", id);
        userVocAISituationOptionMapper.delete(userVocAISituationOptionDOQueryWrapper);
        userVocAISituationMapper.deleteById(id);
    }


    //删除AI阅读场景中的某个选项(后台等级特权管理-AI阅读)
    @Override
    public void deleteAISituationOption(Long id) throws BatchExecutorException {
        userVocAISituationOptionMapper.deleteById(id);
    }


    //批量删除AI阅读场景(后台等级特权管理-AI阅读)
    @Override
    public void batchDeleteAISituationOption(List<Long> ids) throws BatchExecutorException {
        QueryWrapper<UserVocAISituationOptionDO> userVocAISituationOptionDOQueryWrapper = new QueryWrapper<>();
        userVocAISituationOptionDOQueryWrapper.in("situation_id", ids);
        userVocAISituationOptionMapper.delete(userVocAISituationOptionDOQueryWrapper);
        userVocAISituationMapper.deleteByIds(ids);
    }


    //多表查询
    //分页查询AI阅读场景信息(后台等级特权管理-AI阅读)
    @Override
    public PageDTO<UserVocAISituationDTO> getAISituationList(PageRequest pageRequest) {
        PageDTO<UserVocAISituationDTO> pageDTO;
        QueryWrapper<UserVocAISituationDO> userVocAISituationDOQueryWrapper = new QueryWrapper<>();
        if (pageRequest.getKeyword() != null) {
            userVocAISituationDOQueryWrapper.like("name", pageRequest.getKeyword());
        }
        if (pageRequest.getPageNum() != null && pageRequest.getPageNum() > 0) {
            Page<UserVocAISituationDO> page = new Page<>(pageRequest.getPageNum(), pageRequest.getCount());
            Page<UserVocAISituationDO> userVocAISituationDOPage = userVocAISituationMapper.selectPage(page, userVocAISituationDOQueryWrapper);
            List<UserVocAISituationDO> records = userVocAISituationDOPage.getRecords();
            //查询每个模式的选项
            for (UserVocAISituationDO record : records) {
                QueryWrapper<UserVocAISituationOptionDO> userVocAISituationOptionDOQueryWrapper = new QueryWrapper<>();
                userVocAISituationOptionDOQueryWrapper.eq("situation_id",record.getId());
                List<UserVocAISituationOptionDO> userVocAISituationOptionDOS = userVocAISituationOptionMapper.selectList(userVocAISituationOptionDOQueryWrapper);
                record.setOptions(userVocAISituationOptionDOS);
            }
            pageDTO = aiReadConverter.toUserVocAISituationPage(records,userVocAISituationDOPage.getTotal());
        }
        else {
            List<UserVocAISituationDO> userVocAISituationDOS = userVocAISituationMapper.selectList(userVocAISituationDOQueryWrapper);
            //查询每个模式的选项
            for (UserVocAISituationDO userVocAISituationDO : userVocAISituationDOS) {
                QueryWrapper<UserVocAISituationOptionDO> userVocAISituationOptionDOQueryWrapper = new QueryWrapper<>();
                userVocAISituationOptionDOQueryWrapper.eq("situation_id",userVocAISituationDO.getId());
                userVocAISituationDO.setOptions(userVocAISituationOptionMapper.selectList(userVocAISituationOptionDOQueryWrapper));
            }
            pageDTO = aiReadConverter.toUserVocAISituationPage(userVocAISituationDOS,(long)userVocAISituationDOS.size());
        }
        return pageDTO;
    }
}
